// packageJ/projectManage/directly/directly.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadMore: true,
    page: 1,
    levelId:'',
    totalPage: 0,
    active:0,
    gradeArr:[],
    directlyList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData();
    this.teamIndex();
  },
  initData() {
    this.setData({
      levelId:'',
      page:1
    })
  },
  directlyOn(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      [`directlyList[${index}].checked`]:!this.data.directlyList[index].checked
    })
  },
  directlyTab(event) {
    this.setData({
      page:1,
      levelId:event.detail.name
    })
    this.teamIndex();
  },
  teamIndex() {
    let query = {
      basic_info:1,
      level_id:this.data.levelId,
      page:this.data.page
    };
    let urlStr = app.getNetAddresss("plugin.manager-team.frontend.modules.manager.controllers.records.index");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data.page_list;
        let directlyList = data.data;
        directlyList.forEach((item,index) => {
          item.checked = false;
        })
        if (res.result == 1) {
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            directlyList: directlyList,
            managerName:res.data.manager_name,
            gradeArr:res.data.level_list
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        basic_info:1,
        level_id:this.data.levelId,
        page:this.data.page
      };
      let urlStr = app.getNetAddresss("plugin.manager-team.frontend.modules.manager.controllers.records.index");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.page_list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            list.forEach((item,index) => {
              item.checked = false;
            })
            this.setData({
              directlyList : this.data.directlyList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})